const app = getApp();
const util = require('../../utils/util.js');
let PAGE = {page: 1, page_size: 10};
Page({
    data: {
        // 订单总数量
        total: 0,
        // 存储商户信息
        nearyShops: []
    },
    onLoad() {
        PAGE = {page: 1, page_size: 10};
        // 获取附近商家信息
        this.getNearyShopInfo()
    },
    // ============================ 获取附近商家信息 ==============================
    getNearyShopInfo() {
        let that = this;
        my.showLoading({
            content: '加载中...'
        });
        let newList = [];
        let oldList = that.data.nearyShops;
        util.httpRequest('/getShopList', {
            longitude: app.globalData.longitude,
            latitude: app.globalData.latitude,
            page: PAGE.page,
            page_size: PAGE.page_size,
            isList: 1
        }, function (res) {
            if (res.code == 1) {
                that.setData({
                    total: res.data.total
                })
                if (PAGE.page > 1) {//合并原来的数据
                    newList = res.data.list;//新请求的数据
                    that.setData({
                        nearyShops: oldList.concat(newList)
                    })
                } else {
                    // 初始化数据
                    that.setData({
                        nearyShops: res.data.list
                    })
                }
            }
            my.hideLoading();
        }, function () {
            my.hideLoading();
        })
    },

    // ============== 跳转商家界面 ======================
    goSeller(e) {
        let id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '../seller/seller?id=' + id
        })
    },
    // ============== 获取下一页数据 =======================
    onReachBottom() {
        let that = this;
        if (that.data.total > that.data.nearyShops.length) {
            PAGE.page++;
            that.getNearyShopInfo();
        } else {
            return
        }
    },
    // =============  跳转商家界面 =========================
    goToShop(e) {
        let id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '../shop/shop?id=' + id
        })
    },

});
